package apms

import (
	"context"
	"gitee.com/lailonghui/vehicle-supervision-framework/pkg/asserts"
	"gitee.com/lailonghui/vehicle-supervision-framework/pkg/loggers"
	"github.com/opentracing/opentracing-go"
	"github.com/uber/jaeger-client-go/config"
)

func InitOpenTracing(serviceName string, tracingConfig OpenTracingConfig) {
	if !tracingConfig.Enable {
		loggers.Info("不配置opentracing", context.Background())
		return
	}
	// 根据配置初始化Tracer 返回Closer
	tracer, _, err := (&config.Configuration{
		ServiceName: serviceName,
		Disabled:    false,
		Sampler: &config.SamplerConfig{
			Type: tracingConfig.Sampler.Type,
			// param的值在0到1之间，设置为1则将所有的Operation输出到Reporter
			Param: tracingConfig.Sampler.Param,
		},
		Reporter: &config.ReporterConfig{
			LogSpans:            tracingConfig.Reporter.LogSpans,
			BufferFlushInterval: tracingConfig.Reporter.BufferFlushInterval,
			LocalAgentHostPort:  tracingConfig.Reporter.LocalAgentHostPort,
		},
	}).NewTracer()
	asserts.Nil(err, err)

	// 设置全局Tracer - 如果不设置将会导致上下文无法生成正确的Span
	opentracing.SetGlobalTracer(tracer)
	return
}
